package cn.com.winning.ecare.activity;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import yxt.push.org.androidpn.client.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**账号管理
 * @author Administrator
 *
 */
public class AccountManage extends Activity
{
	private RelativeLayout grxx,gywm,bbgx,mzsm,xgmm;
	private AccountManage oThis = this;
	private CustomProgressDialog proDialog;// 加载框
	
	private ImageButton menuButton;
	private ImageButton mainselected;
	private TextView bbxx;
	MyApplication myApplication;
	private ImageView messageSwitch;
	private String deviceid;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.accountmanage);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		SharedPreferences sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		deviceid = sharedPrefs.getString(Constants.DEVICE_ID, "");
		// 初始化view
		findView();
		//从消息系统中获取推送状态
		getState();
		// 点击事件
		setOnClick();
	}
	
	private void getState() {
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadStateData()).start();

	}

	class LoadStateData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniStateData();
			Looper.loop();
		}
	}

	private void iniStateData() {
		try {
			Message msg = stateHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			String state = "";
			ls.add(new BasicNameValuePair("deviceid", deviceid));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.GETSTATE, ls);
			JSONArray jsonArr;
			if (json != null) {
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					msg.what = 0;
					msg.obj = json.getString("pushstate");
				}else{
					msg.what = -1;
					msg.obj = "系统错误！";
				}
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接错误！";
			}
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取pushstate
	 */
	Handler stateHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					if(msg.obj!=null){
						if(msg.obj.equals("2")){
							messageSwitch.setImageResource(R.drawable.off);
						}else{
							messageSwitch.setImageResource(R.drawable.on);
						}
					}
					
					break;
				default:
					break;
			}
		};
	};
	

	private void setOnClick()
	{
		//关于我们
		gywm.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Companycontent.class);
				startActivity(intent);

			}
		});
		//免责声明
		mzsm.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Disclaimercontent.class);
				startActivity(intent);

			}
		});
		//个人信息维护
		grxx.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, UserFamilyActivity.class);
				startActivity(intent);

			}
		});
		//修改密码
		xgmm.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Pswmodify.class);
				startActivity(intent);

			}
		});
//		//版本更新
//		gywm.setOnClickListener(new View.OnClickListener()
//		{
//
//			@Override
//			public void onClick(View v)
//			{
//				UpdateManager updateManager = new UpdateManager(AccountManage.this);
//				// 检查软件更新
//				updateManager.checkUpdate(1);
//
//			}
//		});

		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});
		mainselected.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});
		messageSwitch.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				// TODO Auto-generated method stub
				changeState();
			}
		});
	}

	private void findView()
	{
		gywm = (RelativeLayout) this.findViewById(R.id.gywm);
		grxx = (RelativeLayout) this.findViewById(R.id.grxx);
		mzsm = (RelativeLayout) this.findViewById(R.id.mzsm);
		xgmm = (RelativeLayout) this.findViewById(R.id.xgmm);
//		bbgx = (RelativeLayout) this.findViewById(R.id.bbgx);
		menuButton = (ImageButton) findViewById(R.id.menuButton);
		mainselected = (ImageButton) this.findViewById(R.id.mainselected);
		bbxx = (TextView)this.findViewById(R.id.bbxx);
		bbxx.setText(myApplication.getVersion());
		messageSwitch = (ImageView)this.findViewById(R.id.message_switch);
	}
	
	
	private void changeState() {
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData() {
		try {
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			String state = "";
			if(messageSwitch.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.on).getConstantState())){
				state = "2";//关
			}else{
				state = "1";//开
			}
		
			ls.add(new BasicNameValuePair("pushstate", state));
			ls.add(new BasicNameValuePair("deviceid", deviceid));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.CHANGESTATE, ls);
			JSONArray jsonArr;
			if (json != null) {
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					msg.what = 0;
					msg.obj = state;
				}else{
					msg.what = -1;
					msg.obj = "系统错误！";
				}
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接错误！";
			}
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					if(msg.obj!=null){
						if(msg.obj.equals("2")){
							messageSwitch.setImageResource(R.drawable.off);
						}else{
							messageSwitch.setImageResource(R.drawable.on);
						}
					}
					
					break;
				default:
					break;
			}
		};
	};

}
